package com.bcd.auctions;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bcd.beans.Article;
import com.bcd.beans.UserBean;
import com.bcd.model.ArticleManager;
import com.bcd.model.ArticleType;
import com.bcd.model.Searcher;
import com.bcd.model.UserManager;

/**
 * Servlet implementation class Guest
 */
public class Guest extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private ArticleManager _articles;	
	private ArrayList<Article> _articleList;
	private Article _article;
	private Searcher searcher;
	private RequestDispatcher _dispatcher;
    private HttpSession _session;
    private String _dispatcher_target;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Guest() {
        super();
       _articles = new ArticleManager();
       _dispatcher_target = "index.jsp";
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		_session = request.getSession(true);		
		String type = "";
		String show_page = "articles";		
		
		String action = request.getParameter("action");
		
		if(action != null){
			
			if(action.equals("register")){				
				if(request.getParameter("save") != null){
					UserManager manager = new UserManager();
					UserBean user = new UserBean();
					user.setEmail(request.getParameter("user_name"));
					user.setFirstName(request.getParameter("user_fname"));
					user.setLastName(request.getParameter("user_lname"));
					user.setEmail(request.getParameter("user_email"));	
					user.setPassword(request.getParameter("user_password"));
					user.setStreet(request.getParameter("user_street"));
					user.setPlz(request.getParameter("user_plz"));
					user.setCity(request.getParameter("user_city"));
					user.setCountry(request.getParameter("user_country"));
					manager.SaveNewUser(user);
					show_page="thx";
				}else{
					show_page="register";
				}
				
			}else if(action.equals("search")){
				
				searcher = new Searcher();
				this.SearchArticles(request.getParameter("search_term"), request.getParameter("search_type"));
				_session.setAttribute("articleList", _articleList);
				
			}else if(action.equals("category")){
				
				if(request.getParameter("type") != null){					
					type = request.getParameter("type");					
					this.FetchArticleList(ArticleType.valueOf(type));
				}
								
				_session.setAttribute("articleList", _articleList);

			}else if(action.equals("article")){
				
				this.FetchArticle(Integer.parseInt(request.getParameter("id")));
				_session.setAttribute("article", _article);
				switch(_article.get_articleType()){
					case BOOK:
						show_page="detail_book";
						break;
					case CD:
						show_page="detail_cd";
						break;
					case DVD:
						show_page="detail_dvd";
						break;
				}
				
			}else if(action.equals("register")){
				
				_dispatcher_target="/Register";
				
			}
		}else{ // alle artikel zeigen			
			this.FetchArticleList(ArticleType.ALL);
			_session.setAttribute("articleList", _articleList);
		}
		
		_session.setAttribute("show_page", show_page);
		_dispatcher = request.getRequestDispatcher(_dispatcher_target);
		_dispatcher.forward( request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
	
	private void FetchArticleList(ArticleType type){
		_articleList = _articles.GetArticleList(type);
	}
	private void FetchArticle(int article_id){
		_article = _articles.GetArticle(article_id);
	}
	private void SearchArticles(String term, String type){
		_articleList = searcher.Search(term, type);
	}

}
